/********************************************************************************
  * @file     SD93F115B_vic.c   						    	
  * @author   TEST TEAM 												    	
  * @version  v0															    	
  * @date     Septemper 2022												    	
  * @brief 
  *           
  * Copyright (C) 2022  Hangzhou SDIC Microelectronics Co., Ltd  
*********************************************************************************/
/* Includes */
#include "SD93F115B_vic.h"

/*----------------------------------------
 * 函数功能：VIC相关寄存器恢复成上电默认值
 * 入口参数：None
 * 出口参数：None
 * 返回值  ：None
 * 编写日期：
 * 作者    ：
 *----------------------------------------*/
void VIC_DeInit(void)
{
	VIC_ICER=0xffffffff;
	VIC_IWDR=0xffffffff;
	VIC_ICPR=0xffffffff;
	VIC_IABR=0;
	
	VIC_IPR0=0;
	VIC_IPR1=0;
	VIC_IPR2=0;
	VIC_IPR3=0;
	VIC_IPR4=0;
	VIC_IPR5=0;
	VIC_IPR6=0;
	VIC_IPR7=0;	
	
	VIC_IPTR=0;
}
/*-----------------------------------------------
 * 函数功能：VIC初始化
 * 入口参数：VIC_InitStruct:pointer to a VIC_InitStruct structure that
 *           contains the configuration information for the specified VIC peripheral.
 * 出口参数：None
 * 返回值  ：None
 * 编写日期：
 * 作者    ：
 *-----------------------------------------------*/
 void VIC_Init(VIC_InitTypeDef* VIC_InitStruct)
 {
    uint8_t tmpreg=0,tmpreg1=0,tmpreg2=0;
	/* Check the parameters */
	assert_param(IS_VIC_IRQn(VIC_InitStruct->VIC_IRQChannel)); 
	assert_param(IS_VIC_Priority(VIC_InitStruct->VIC_IRQChannelPriority)); 
	assert_param(IS_VIC_PrioThreshold(VIC_InitStruct->VIC_PrioThreshold));	
	assert_param(IS_FUNCTIONAL_STATE(VIC_InitStruct->VIC_PrioThresholdCmd));
	assert_param(IS_FUNCTIONAL_STATE(VIC_InitStruct->VIC_IRQChannelWakeUpLowPowerCmd));
	assert_param(IS_FUNCTIONAL_STATE(VIC_InitStruct->VIC_IRQChannelCmd));

	tmpreg=VIC_InitStruct->VIC_IRQChannel;
	tmpreg1=VIC_InitStruct->VIC_IRQChannelPriority;
	tmpreg2=VIC_InitStruct->VIC_PrioThreshold;
	
	VIC_IRQChannelPriorityConfig(tmpreg,tmpreg1); 
	VIC_PrioThresholdConfig(tmpreg,tmpreg2);
	
	VIC_ClearIRQChannelPendingFlagStatus(tmpreg);
	VIC_ClearIRQChannelActiveFlagStatus(tmpreg);

    VIC_PrioThresholdCmd(VIC_InitStruct->VIC_PrioThresholdCmd);  

    VIC_IRQChannelWakeUpLowPowerCmd(tmpreg,VIC_InitStruct->VIC_IRQChannelWakeUpLowPowerCmd);
	
	VIC_IRQChannelCmd (tmpreg,VIC_InitStruct->VIC_IRQChannelCmd);

}

 /*------------------------------------------------------
 * 函数功能：配置中断优先级
 * 入口参数：VIC_IRQChannel,VIC_IRQChannelPriority
 * 出口参数：None
 * 返回值  ：None
 * 编写日期：
 * 作者    ：
 *-------------------------------------------------------*/
void VIC_IRQChannelPriorityConfig(IRQn_Type VIC_IRQChannel,VICPriority_TypeDef VIC_IRQChannelPriority) 
{
	
	uint8_t tmpreg=0,tmpreg2=0;
	uint32_t tmpreg1=0;
	/* Check the parameters */
	assert_param(IS_VIC_IRQn(VIC_IRQChannel)); 
	assert_param(IS_VIC_Priority(VIC_IRQChannelPriority)); 

	tmpreg=VIC_IRQChannel;
	tmpreg2=6+8*(tmpreg%4);
	
	switch(tmpreg/4)
	{
		case 0:	
		    tmpreg1=VIC_IPR0;
			tmpreg1&=~(((uint32_t)0x03)<<tmpreg2);
			tmpreg1|=((uint32_t)VIC_IRQChannelPriority)<<tmpreg2;
			VIC_IPR0=tmpreg1;
			break;
			
		case 1:
		    tmpreg1=VIC_IPR1;
			tmpreg1&=~(((uint32_t)0x03)<<tmpreg2);
			tmpreg1|=((uint32_t)VIC_IRQChannelPriority)<<tmpreg2;
			VIC_IPR1=tmpreg1;
			break;
			
		case 2:
		    tmpreg1=VIC_IPR2;
			tmpreg1&=~(((uint32_t)0x03)<<tmpreg2);
			tmpreg1|=((uint32_t)VIC_IRQChannelPriority)<<tmpreg2;
			VIC_IPR2=tmpreg1;
			break;
			
		case 3:
		    tmpreg1=VIC_IPR3;
			tmpreg1&=~(((uint32_t)0x03)<<tmpreg2);
			tmpreg1|=((uint32_t)VIC_IRQChannelPriority)<<tmpreg2;
			VIC_IPR3=tmpreg1;
			break;
			
		case 4:
		    tmpreg1=VIC_IPR4;
			tmpreg1&=~(((uint32_t)0x03)<<tmpreg2);
			tmpreg1|=((uint32_t)VIC_IRQChannelPriority)<<tmpreg2;
			VIC_IPR4=tmpreg1;
			break;
			
		case 5:
		    tmpreg1=VIC_IPR5;
			tmpreg1&=~(((uint32_t)0x03)<<tmpreg2);
			tmpreg1|=((uint32_t)VIC_IRQChannelPriority)<<tmpreg2;
			VIC_IPR5=tmpreg1;
			break;
			
		case 6:
		    tmpreg1=VIC_IPR6;
			tmpreg1&=~(((uint32_t)0x03)<<tmpreg2);
			tmpreg1|=((uint32_t)VIC_IRQChannelPriority)<<tmpreg2;
			VIC_IPR6=tmpreg1;			
			break;
			
		case 7:
		    tmpreg1=VIC_IPR7;
			tmpreg1&=~(((uint32_t)0x03)<<tmpreg2);
			tmpreg1|=((uint32_t)VIC_IRQChannelPriority)<<tmpreg2;
			VIC_IPR7=tmpreg1;
			break;
	
		default:
			break;					
	}
}

 /*---------------------------------------------
 * 函数功能：配置中断优先级的阈值
 * 入口参数：VIC_IRQChannel,VIC_PrioThreshold
 * 出口参数：None
 * 返回值  ：None
 * 编写日期：
 * 作者    ：
 *-----------------------------------------------*/
void VIC_PrioThresholdConfig(IRQn_Type VIC_IRQChannel,VICPrioThreshold_TypeDef VIC_PrioThreshold)
{
	/* Check the parameters */ 
	assert_param(IS_VIC_IRQn(VIC_IRQChannel)); 
	assert_param(IS_VIC_PrioThreshold(VIC_PrioThreshold));
	assert_param(IS_FUNCTIONAL_STATE(NewState));
	
	uint8_t tmpreg=0;
	uint32_t tmpreg1=0;
	
	tmpreg=VIC_IRQChannel;
	tmpreg1=VIC_IPTR&VIC_IPTR_EN; 
	tmpreg1|=(VIC_PrioThreshold<<6|(((uint32_t)tmpreg)<<8));
	VIC_IPTR=tmpreg1;
	
} 
 /*---------------------------------------------
 * 函数功能：使能中断优先级的阈值功能
 * 入口参数：NewState: ENABLE或者DISABLE
 * 出口参数：None
 * 返回值  ：None
 * 编写日期：
 * 作者    ：
 *-----------------------------------------------*/
void VIC_PrioThresholdCmd(FunctionalState NewState)
{
	/* Check the parameters */ 
	assert_param(IS_FUNCTIONAL_STATE(NewState));
	
	if(NewState == ENABLE)
		VIC_IPTR|=VIC_IPTR_EN;
	else
		VIC_IPTR&=~VIC_IPTR_EN;
}
 /*---------------------------------------------
 * 函数功能：使能对应编号的中断通道
 * 入口参数：VIC_IRQChannel,NewState
 * 出口参数：None
 * 返回值  ：None
 * 编写日期：
 * 作者    ：
 *-----------------------------------------------*/
void VIC_IRQChannelCmd (IRQn_Type VIC_IRQChannel,FunctionalState NewState)
{
	/* Check the parameters */
	assert_param(IS_VIC_IRQn(VIC_IRQChannel)); 
	assert_param(IS_FUNCTIONAL_STATE(NewState));	
	uint32_t tmpreg;
	if (NewState == ENABLE)
	{	
		VIC_ISER|=(((uint32_t)0x01)<<VIC_IRQChannel);							
	} 
	else 
	{	
		tmpreg = VIC_ISER;
		VIC_ICER|=(((uint32_t)0x01)<<VIC_IRQChannel);
		tmpreg = tmpreg &(~(((uint32_t)0x01)<<VIC_IRQChannel));
		VIC_ISER = tmpreg;
	}	
}

/*---------------------------------------
 * 函数功能：使能中断唤醒低功耗功能
 * 入口参数：VIC_IRQChannel,NewState
 * 出口参数：None
 * 返回值  ：None
 * 编写日期：
 * 作者    ：
 *---------------------------------------*/
void VIC_IRQChannelWakeUpLowPowerCmd(IRQn_Type VIC_IRQChannel,FunctionalState NewState)
{
	/* Check the parameters */	
	assert_param(IS_VIC_IRQn(VIC_IRQChannel)); 
	assert_param(IS_FUNCTIONAL_STATE(NewState));	
	
	if (NewState == ENABLE)
	{	
		VIC_IWER|=(((uint32_t)0x01)<<VIC_IRQChannel);							
	} 
	else 
	{
		VIC_IWDR =(((uint32_t)0x01)<<VIC_IRQChannel);		
	} 		
}

/*-----------------------------
 * 函数功能：设置对应编号的中断为挂起状态
 * 入口参数：VIC_IRQChannel
 * 出口参数：None
 * 返回值  ：None
 * 编写日期：
 * 作者    ：
 *------------------------------*/
void VIC_SetIRQChannelPendingStatus(IRQn_Type VIC_IRQChannel)                           
{
	/* Check the parameters */	
	assert_param(IS_VIC_IRQn(VIC_IRQChannel)); 
	
	VIC_ISPR|=(((uint32_t)0x01)<<VIC_IRQChannel);							
	
}

/*--------------------------------------
 * 函数功能：获取对应编号的中断挂起状态
 * 入口参数：VIC_IRQChannel
 * 出口参数：None
 * 返回值  ：None
 * 编写日期：
 * 作者    ：
 *--------------------------------------*/
FlagStatus VIC_GetIRQChannelPendingFlagStatus(IRQn_Type VIC_IRQChannel)                   
{
	/* Check the parameters */	
	assert_param(IS_VIC_IRQn(VIC_IRQChannel)); 
	
	FlagStatus bitstatus = RESET;
	
	if(VIC_ISPR&(((uint32_t)0x01)<<VIC_IRQChannel))
	{		
		bitstatus =	SET;		
	}		
	else 
	{
		bitstatus = RESET;		
	}
	
	return bitstatus;								
}

/*------------------------------------------
 * 函数功能：清除对应编号的中断挂起状态
 * 入口参数：VIC_IRQChannel
 * 出口参数：None
 * 返回值  ：None
 * 编写日期：
 * 作者    ：
 *------------------------------------------*/
void VIC_ClearIRQChannelPendingFlagStatus(IRQn_Type VIC_IRQChannel)
{
	/* Check the parameters */	
	assert_param(IS_VIC_IRQn(VIC_IRQChannel)); 
	
	VIC_ICPR|=(((uint32_t)0x01)<<VIC_IRQChannel);							
	
}

/*-------------------------------------------
 * 函数功能：获取对应编号的中断响应状态
 * 入口参数：VIC_IRQChannel
 * 出口参数：None
 * 返回值  ：None
 * 编写日期：
 * 作者    ：
 *-------------------------------------------*/
FlagStatus VIC_GetIRQChannelActiveFlagStatus(IRQn_Type VIC_IRQChannel)
{
	/* Check the parameters */	
	assert_param(IS_VIC_IRQn(VIC_IRQChannel)); 
	
	FlagStatus bitstatus = RESET;
	
	if(VIC_IABR&(((uint32_t)0x01)<<VIC_IRQChannel))
	{		
		bitstatus =	SET;		
	}		
	else 
	{
		bitstatus = RESET;		
	}
	
	return bitstatus;	
}

/*---------------------------------------
 * 函数功能：清除对应编号的中断响应状态
 * 入口参数：VIC_IRQChannel
 * 出口参数：None
 * 返回值  ：None
 * 编写日期：
 * 作者    ：
 *---------------------------------------*/
void VIC_ClearIRQChannelActiveFlagStatus(IRQn_Type VIC_IRQChannel)
{
	/* Check the parameters */	
	assert_param(IS_VIC_IRQn(VIC_IRQChannel)); 
	
	VIC_ICPR&=~(((uint32_t)0x01)<<VIC_IRQChannel);	
}
 

/*------------------------------------------------
 * 函数功能：获取当前挂起的优先级最高的中断向量号
 * 入口参数：None
 * 出口参数：tmpreg
 * 返回值  ：当前挂起的优先级最高的中断向量号
 * 编写日期：
 * 作者    ：
 *------------------------------------------------*/
uint16_t VIC_GetVECTPendingStatus(void)                                                
{
	uint16_t tmpreg=0;
	tmpreg=(VIC_ISR&0x001FF000)>>11;
	return tmpreg;	
}


/*----------------------------------------------
 * 函数功能：获取CPU当前正在处理的中断向量号
 * 入口参数：None
 * 出口参数：tmpreg
 * 返回值  ：CPU当前正在处理的中断向量号
 * 编写日期：
 * 作者    ：
 *-----------------------------------------------*/
uint16_t VIC_GetVECTActiveStatus(void)
{
	uint16_t tmpreg=0;
	tmpreg=VIC_ISR&0x000001FF;
	return tmpreg;			
}


/****************************END OF FILE****************************/